home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / RAVE Starter Samples / Gouraud Sample / Gouraud Triangle.cp next >
Encoding:
Text File  |  1998-04-30  |  5.8 KB  |  192 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. Gouraud Triangles.cp
  3. Author: Timothy Carroll
  4. Apple Developer Technical Support
  5. devsupport@apple.com
  6.  
  7. Modification History: 
  8.  
  9. 5/1/98        TMC     Initial Release
  10.  
  11. Copyright © 1998 Apple Computer, Inc., All Rights Reserved
  12.  
  13. You may incorporate this sample code into your applications without
  14. restriction, though the sample code has been provided "AS IS" and the
  15. responsibility for its operation is 100% yours.  However, what you are
  16. not permitted to do is to redistribute the source as "DSC Sample Code"
  17. after having made changes. If you're going to re-distribute the source,
  18. we require that you make it clear in the source that the code was
  19. descended from Apple Sample Code, but that you've made changes.
  20. *************************************************************************************/
  21.  
  22. #include <RAVE.h>
  23. #include <QD3DAcceleration.h>  // We need this for engine gestalt codes
  24. #include "Common Stuff.h"
  25. #include "RAVE Utilities.h"
  26.  
  27.  
  28. // Standard declarations
  29. void main (void);
  30. void DoRAVEWindow(void);
  31.  
  32.  
  33. void main (void)
  34. {
  35.     // do standard mac init
  36.     InitGraf(&qd.thePort);
  37.     InitFonts();
  38.     InitWindows();
  39.     InitMenus();
  40.     TEInit();
  41.     InitDialogs(nil);
  42.     InitCursor();
  43.  
  44.     MaxApplZone();
  45.     MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters();
  46.  
  47.     // Initialize the SIN and COS lookup tables (see RAVE Utilities.cp)
  48.     if (InitializeLookups() == noErr)
  49.         DoRAVEWindow();
  50. }
  51.  
  52.  
  53. void DoRAVEWindow(void)
  54. {
  55.     // These hold our errors
  56.     OSErr            theErr = noErr;
  57.     TQAError        theQAErr = kQANoErr;
  58.     
  59.     // These hold the window and device we'll put on the screen
  60.     GDHandle        device = NULL;
  61.     WindowRef        theWindow = NULL;
  62.     
  63.     // We try to center the window on the device.
  64.     Rect            bounds = {0,0,320,320};
  65.     Rect            deviceRect;
  66.     
  67.     // We use these parameters to describe our environment to RAVE
  68.     TQADevice        qaDevice;
  69.     TQARect         qaBoundsRect;
  70.     
  71.     // we get these items back from RAVE
  72.     TQAEngine         *theEngine = NULL;
  73.     TQADrawContext    *theContext = NULL;
  74.     
  75.     // We use these in our drawing loop to create our RAVE animation.
  76.     int             loop = 0;
  77.     TQAVGouraud     theTriangle[3];
  78.     
  79.     RgnHandle        gray;
  80.     Rect            grayRect;
  81.     
  82.     
  83.     // First, grab the first monitor with the deepest display
  84.     gray = GetGrayRgn();
  85.     grayRect = (**gray).rgnBBox;
  86.     device = GetMaxDevice(&grayRect);
  87.     FAIL_NIL (device, "\pERROR: No monitors found")
  88.     
  89.     // Center our bounds in the middle of the screen.
  90.     deviceRect = (**device).gdRect;
  91.  
  92.     OffsetRect (&bounds,
  93.                     (deviceRect.right+deviceRect.left-bounds.right)/2,
  94.                     (deviceRect.bottom+deviceRect.top-bounds.bottom)/2);
  95.     
  96.     // Create a Window to hold the RAVE information.
  97.         
  98.     theWindow = NewCWindow(NULL, &bounds, "\pRAVE Window", true, zoomDocProc, (WindowRef) -1, false, 0);
  99.     FAIL_NIL (theWindow, "\pERROR: Couldn't create RAVE Window")
  100.     
  101.     SetPortWindowPort (theWindow);
  102.     
  103.     // Next we need to find a RAVE engine and then build a DrawContext to draw into it.  First, we'll find
  104.     // an engine that is capable of drawing to the selected GDevice.
  105.  
  106.     qaDevice.deviceType = kQADeviceGDevice;
  107.     qaDevice.device.gDevice = device;
  108.     
  109.     // We have to explicitly enable the Apple hardware card because it isn't 100% RAVE compliant.
  110.     // If we use this engine, we have to write special case code to deal with it.
  111. #if qEnableAppleHardware
  112.     QAEngineEnable (kQAVendor_Apple,kQAEngine_AppleHW);
  113. #endif
  114.     
  115.     // We'll just grab the first engine that the system offers us.
  116.  
  117.     theEngine = QADeviceGetFirstEngine (&qaDevice);
  118.     FAIL_NIL (theEngine, "\pERROR:  No RAVE engines available.")
  119.     
  120.     // Next we need to set up the a QARect with the Window's rectangle in GDevice local coordinates.
  121.     
  122.     qaBoundsRect.left = bounds.left - deviceRect.left;
  123.     qaBoundsRect.right = bounds.right  - deviceRect.left;
  124.     qaBoundsRect.top = bounds.top - deviceRect.top;
  125.     qaBoundsRect.bottom = bounds.bottom - deviceRect.top;
  126.  
  127.     // If we wanted to create a clipping region, we'd do it here
  128.     
  129.     // Finally, we're ready to create the draw context!
  130.     theQAErr = QADrawContextNew (&qaDevice, &qaBoundsRect, NULL, theEngine, 
  131.                                  kQAContext_DeepZ | kQAContext_DoubleBuffer, &theContext);
  132.     if (theQAErr != kQANoErr) SIGNAL_ERROR("\pERROR:  Failed to create RAVE draw context")
  133.     FAIL_NIL (theContext, "\pERROR:  Failed to create RAVE draw context")
  134.     
  135.     // set the background to black
  136.     QASetFloat (theContext, kQATag_ColorBG_a, 1.0);
  137.     QASetFloat (theContext, kQATag_ColorBG_r, 0.0);
  138.     QASetFloat (theContext, kQATag_ColorBG_g, 0.0);
  139.     QASetFloat (theContext, kQATag_ColorBG_b, 0.0);
  140.     
  141.     // Finally, everything is ready!  We'll create an animated sequence and end it when the user
  142.     // presses a button.
  143.     
  144.  
  145.     while (!Button())
  146.     {
  147.     // Set up our points
  148.     theTriangle[0].x = 160 + 150*gCosArray[loop];
  149.     theTriangle[0].y = 160 + 150*gSinArray[loop];
  150.     theTriangle[0].z = 0.25;
  151.     theTriangle[0].invW = 1.0;
  152.     theTriangle[0].a = 1;
  153.     theTriangle[0].r = 1.0;
  154.     theTriangle[0].g = 0.0;
  155.     theTriangle[0].b = 0.0;
  156.     
  157.     theTriangle[1].x = 160 + 150*gCosArray[(loop+240) % 720];
  158.     theTriangle[1].y = 160 + 150*gSinArray[(loop+240) % 720];
  159.     theTriangle[1].z = 0.25;
  160.     theTriangle[1].invW = 1.0;
  161.     theTriangle[1].a = 1;
  162.     theTriangle[1].r = 0.0;
  163.     theTriangle[1].g = 1.0;
  164.     theTriangle[1].b = 0.0;    
  165.     
  166.     theTriangle[2].x = 160 + 150*gCosArray[(loop+480) % 720];
  167.     theTriangle[2].y = 160 + 150*gSinArray[(loop+480) % 720];
  168.     theTriangle[2].z = 0.25;
  169.     theTriangle[2].invW = 1.0;
  170.     theTriangle[2].a = 1;
  171.     theTriangle[2].r = 0.0;
  172.     theTriangle[2].g = 0.0;
  173.     theTriangle[2].b = 1.0;    
  174.  
  175.     // Render the triangle.
  176.     QARenderStart (theContext, NULL, NULL);
  177.     QADrawTriGouraud (theContext, &theTriangle[0], &theTriangle[1], &theTriangle[2], kQATriFlags_None);
  178.     QARenderEnd(theContext, NULL);    
  179.     
  180.     // Increment our loop position 2.5 degrees
  181.     loop = (loop+5) % 720;
  182.     }
  183.     
  184.     error:
  185.     // Either we got an error or the user finished.  In any case, tear everything down and return
  186.     
  187.     if (theContext != NULL)
  188.         QADrawContextDelete (theContext);
  189.     if (theWindow != NULL)
  190.         DisposeWindow(theWindow);
  191.     return;
  192. }